<?php
  require_once('./db.php');

  $file = $_SERVER['PHP_SELF'];
  $id = isset($_GET['id'])?$_GET['id']:0;
  $msg = '';
  //?status=0&mode=status&id=5
  if(isset($_GET['mode']) &&
  $_GET['mode']=='status') {
    $newStatus = $_GET['status']==1?0:1;
    $updateQuery = "UPDATE `users` SET
    `status`='$newStatus'
    WHERE `id`='$id'";
    pq($updateQuery);
    mysql_query($updateQuery) or die(mysql_error());
    if(mysql_affected_rows() == 1){
      $msg = 'Status updated  successfully';
    }
  }
  if(isset($_GET['mode']) &&
  $_GET['mode']=='del') {

    $delQuery = "DELETE FROM `users`
    WHERE `id`='$id'";
    mysql_query($delQuery);
    if(mysql_affected_rows()==1){
      echo "Record Deleted Successfully";
    }
  }

  $query = "SELECT * FROM `users`";
  $result = mysql_query($query) or die(mysql_error());

?>
<h4><em><?=$msg?></em></h4>
<table  border="1" cellpadding="0" cellspacing="0" width="80%" align="center">
  <thead>
    <tr>
      <th colspan="6">:: All Users  ::</th>
    </tr>
    <tr>
      <th align="left">#</th>
      <th align="left">Name</th>
      <th align="left">Email</th>
      <th align="left">Added date</th>
      <th align="left">Status</th>
      <th align="left">Action</th>
    </tr>
  </thead>
  <tbody>
<?php
  while($ans = mysql_fetch_assoc($result))
  {
    $id = $ans['id'];
    $status = $ans['status'];
?>
    <tr>
      <td><?=$id?></td>
      <td><?=$ans['name']?></td>
      <td><a href="mailto:<?=$ans['email']?>"><?=$ans['email']?></a></td>
      <td><?=date('d-M-Y G:i a',strtotime($ans['added_date']))?></td>

      <td>
        <a href="<?=$file?>?status=<?=$status?>&mode=status&id=<?=$id?>">
        <?=getStatus($status)?>
        </a>
      </td>

      <td>

      <a target="_blank" href="edit_user.php?id=<?=$id?>">Edit</a> /

      <a onClick="return confirm('Are you sure ?');" href="display_users.php?id=<?=$id?>&mode=del">Delete</a>

      </td>
    </tr>
<?php
  }// end while
?>

  </tbody>
</table>
